﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;


namespace CH_k
{
    [Serializable]
    class AND: LoElement
    {     
        public AND(bool _invers = false)
        {
            input_n = 2;
            size = new Point(20, 30);
            invers = _invers;
            ElType = invers ? "NAND" : "AND";
        }
        
        public override bool Run()
        {
            if (!alreadyRun)
            {
                alreadyRun = true;
                int sum = 0;
                for (int i = 0; i < input_n; i++)
                {

                    if (inElList[i] != null)
                    {
                        if (inElList[i].Run()) sum++;
                    }
                }
                oldState = sum == input_n ? true : false;
                if (invers) oldState = !oldState; 
                return oldState;
            }
            else
            {
                return oldState;
            }
        }

        public override void Draw(Graphics _g)
        {
            if(cPen == null) cPen = new Pen(Color.Black);
            for (int i = 0; i < input_n * 10; i += 10)
            {
                _g.DrawLine(cPen, new Point(dec.X, dec.Y + 10 + i), new Point(dec.X + 10, dec.Y + 10 + i));
            }
            _g.DrawRectangle(cPen, dec.X + 10, dec.Y, 20, 10 + input_n * 10);
            _g.FillRectangle(Brushes.LightGray, dec.X + 11, dec.Y + 1, 19, 9 + input_n * 10);
            _g.DrawString("&", new Font("Arial", 12), Brushes.Black, new Point(dec.X + 12, dec.Y));
            _g.DrawLine(cPen, new Point(dec.X + 30, dec.Y + 10), new Point(dec.X + 40, dec.Y + 10));
            if (invers)
            {
                _g.FillEllipse(Brushes.White, dec.X + 27, dec.Y + 7, 6, 6);
                _g.DrawEllipse(cPen, dec.X + 27, dec.Y + 7, 6, 6);
            }
        }
    }
}
